package jge;

/** Tank object class that has
 * fields for all of the attributes
 * that the tank will need.
 * Also has a reverse field
 * for reversing the sprite.
 * 
 * 
 * NOTE FOR USE:
 * If changing an attribute
 * that is used in draw()
 * please note that draw()
 * is a function in object
 * and not TankObject. This
 * means you have to change
 * the attribute in the super
 * class object before it has
 * an effect in draw().
 * 
 * @author Andrew
 * LAST UPDATED - Andrew 16/02/12
 */


public class TankObject extends Object {
    // Health of tank
    private int health;
    
    private boolean alive;
    
    // Sprite Reference for object
    public String sprite = "cata.png";
    
    // Internal timer for animations
    private Timer frameTimer = new Timer();
    
    // Is an Animation playing?
    private boolean animation;
    
    // Do we want an Animation Playing?
    private boolean ani;
    
    // Fire Frames per milisecond
    private int Firefps = 800/10;
    
    // Sprite List for all sprites needed during animation
    public Sprite[] spritelist = { new Sprite("cata1.png"),
                                   new Sprite("cata2.png"),
                                   new Sprite("cata3.png"),
                                   new Sprite("cata4.png"),
                                   new Sprite("cata5.png"),
                                   new Sprite("cata6.png"),
                                   new Sprite("cata7.png"),
                                   new Sprite("cata8.png"),
                                   new Sprite("cata9.png"),
                                   new Sprite("cata10.png")};
    
    /** Space for attributes like armour
     * or inventory
     */
    
    // Is object reversed?
    private boolean reversed = false;
    
    // HealthBar
    private HealthBar hb = new HealthBar(x,y);
    
    // create a new Object with a sprite
  public TankObject(double x, double y,String sprite) {
      super((new Sprite(sprite)),x,y);
      this.animation = false;
      this.ani = false;
      this.health = 5;
      this.alive = true;
  }
  
  // Change the health of this tank
  public void changeHealth(int n) {
      this.health -= n;
      if(health <= 0) {
          this.alive = false;
      }
  }
  
  // Move the health bar of this tank
  public void adjustHealthBar(int xii, int yii) {
      this.hb = new HealthBar(xii, yii);
      this.hb.update();
  }
  
  // draw the tank and its health bar
  public void draw() {
      super.draw();
      hb.draw(this.health);
      
  }
  
  // If the target is Alive return true
  public boolean isAlive() {
      return this.alive;
  }
  
  // Return the health of this tank
  public int getHealth() {
      return this.health;
  }
  
  // reverses object by loading new sprite
  public void reverse() {
      reversed = true;
      super.sprite = new Sprite("catar.png");
  }
  
  // Function which runs a firing animation
  // returns true when it is ok to fire the ball.
  public boolean fire() {
      if(this.animation == false &&
         this.ani == false) {
          frameTimer.reset();
          this.animation = true;
          this.ani = true;
          return false;
      }
      else if(this.animation == true &&
              this.ani == false) {
          return true;
          // but do nothing
      }
      else {
        if(frameTimer.getDeltaTime()<=Firefps) { super.sprite = spritelist[0];  return false; }
        else if(frameTimer.getDeltaTime()>Firefps && frameTimer.getDeltaTime()<=Firefps*2) { super.sprite = spritelist[1]; return false; }
        else if(frameTimer.getDeltaTime()>Firefps*2 && frameTimer.getDeltaTime()<=Firefps*3){ super.sprite = spritelist[2]; return false; }
        else if(frameTimer.getDeltaTime()>Firefps*3 && frameTimer.getDeltaTime()<=Firefps*4){  super.sprite = spritelist[3]; return false; }
        else if(frameTimer.getDeltaTime()>Firefps*4 && frameTimer.getDeltaTime()<=Firefps*5){  super.sprite = spritelist[4]; return false; }
        else if(frameTimer.getDeltaTime()>Firefps*5 && frameTimer.getDeltaTime()<=Firefps*6){  super.sprite = spritelist[5];  return true; }
        else if(frameTimer.getDeltaTime()>Firefps*6 && frameTimer.getDeltaTime()<=Firefps*7){  super.sprite = spritelist[6];  return true; }
        else if(frameTimer.getDeltaTime()>Firefps*7 && frameTimer.getDeltaTime()<=Firefps*8){  super.sprite = spritelist[7];  return true; }
        else if(frameTimer.getDeltaTime()>Firefps*8 && frameTimer.getDeltaTime()<=Firefps*9){  super.sprite = spritelist[8];  return true; }
        else if(frameTimer.getDeltaTime()>Firefps*9 && frameTimer.getDeltaTime()<=Firefps*10){  super.sprite = spritelist[9];  return true; }
        else {
          super.sprite = spritelist[0];
          this.animation = false;
          return true;
      }
      }
  }
  
  // resets the time value, the sprite and the the animation boolean
  public void reset() {
      super.sprite = spritelist[0];
      this.ani = false;
      this.frameTimer.reset();
  }
  
}